package com.zhuhjay.framework.security.service;

import com.zhuhjay.common.core.util.SecurityContextHolder;
import com.zhuhjay.service.system.service.SystemMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.zhuhjay.common.base.constant.ApplicationConstant.SUPER_ADMIN_AUTHORITY;

/**
 * 权限校验, 用于注解@PreAuthorize
 * @author ZhuHJay
 * @date 2023/2/13 22:49
 */
@Service("perm")
public class PermissionService {

    @Resource
    private SystemMenuService menuService;

    public boolean hasPerm(String permission) {
        // 如果是超级管理员, 直接返回true
        if (SecurityContextHolder.isAdmin()) {
            return true;
        }
        // 有些权限只有是`admin`才能操作的
        if (SUPER_ADMIN_AUTHORITY.equals(permission)) {
            return SecurityContextHolder.isAdmin();
        }
        // 判断权限
        return menuService.checkPermission(SecurityContextHolder.getLoginUserDetail().getId(), permission);
    }

}
